package com.example.android.ui.me.user;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.navigation.Navigation;

import com.example.android.R;
import com.example.android.base.BaseFragment2;
import com.example.android.bean.BaseLoginToken;
import com.example.android.bean.Login;
import com.example.android.utils.api.BackApi;
import com.example.android.utils.NetUtil;
import com.example.android.utils.SPUtils;
import com.google.android.material.snackbar.Snackbar;

import java.util.Objects;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginFragment extends BaseFragment2 {


    private EditText editText;
    private EditText editText2;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_login, container, false);

        editText = root.findViewById(R.id.editText);
        editText2 = root.findViewById(R.id.editText2);

        TextView textView = root.findViewById(R.id.textView);
        textView.setOnClickListener(view -> Navigation.findNavController(view).navigate(R.id.action_loginFragment_to_registerFragment));

        Button button = root.findViewById(R.id.button);
        button.setOnClickListener(this::login);

        return root;
    }


    /**
     * 账号密码登录
     */
    private void login(final View view) {
        String username = editText.getText().toString();
        String password = editText2.getText().toString();


        if (TextUtils.isEmpty(username)) {
            editText.setError("用户名不能为空");
            return;
        }

        if (TextUtils.isEmpty(password)) {
            editText.setError("密码不能为空");
            return;
        }
        Login login = new Login(username, password);
        Call<BaseLoginToken> baseModelCall = NetUtil.INSTANCE.get(NetUtil.INSTANCE.getBaseUrlDev()).create(BackApi.class).loginUser(login);
        Log.i("请求成功", "获得的数据" + baseModelCall.toString());
        baseModelCall.enqueue(new Callback<BaseLoginToken>() {
            @Override
            public void onResponse(Call<BaseLoginToken> call, Response<BaseLoginToken> response) {
                Log.i("输出的登录信息", response.body().toString());
                if (Objects.requireNonNull(response.body()).getCode() == 200) {
                    Log.i("输出的token信息", "x"+response.body());
                    SPUtils.putString("token", response.body().getToken());
                    Snackbar.make(view, "登录成功：" + username, Snackbar.LENGTH_LONG).show();
                    Navigation.findNavController(view).navigateUp();
                } else {
                    Snackbar.make(view, "登录失败：" + response.body().getMsg(), Snackbar.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<BaseLoginToken> call, Throwable t) {
                Log.i("请求失败", "请求失败" + t.toString());
                Log.i("请求失败返回数据", call.toString());
            }
        });
    }
}